import {request} from './request.js'

function login(email, password, captcha,idKeyC) {
  return request({
    url: '/login',
    method: 'post',
    data: {
      email,
      password,
      idKeyC,
      captcha: captcha.toLowerCase()
    }
  })
}

// 每次加载页面，获取用户登录态
function getUser() {
  request({
    url: '/getuser',
  }).then(res => {
    if(res.data.code === 0) {
      // 把服务端读出来的token数据保存到vuex
      localStorage.setItem('token', "Bearer " + res.data.data.token)
      this.$store.commit('setUser', res.data.data)
    }
  })
}

function getCaptcha() {
  return request({
    url: '/captcha',
    params:Math.random()
  })
}

export default {
  login,
  getUser,
  getCaptcha
}
